﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;
using ScrumTable.Common;

#endregion

namespace ScrumTable.DL.Data.Generic.Cache
{
    /// <summary>
    /// Specifies the cache state for a save pair. This state is used
    /// in conjuction with the MergeChanges() procedure in the generic data
    /// layer.
    /// </summary>
    public enum CacheSavePairState
    {
        /// <summary>
        /// The save procedure has been completed successfully. Only not
        /// invalidated data should be updated into the local memory.
        /// </summary>
        Saved,

        /// <summary>
        /// The element has been updated on server side. This means, that
        /// the data invalidation must be forced.
        /// </summary>
        Updated,

        /// <summary>
        /// The element has been deleted on server side. You have to remove
        /// it in the local memory too.
        /// </summary>
        Deleted,

        /// <summary>
        /// An error occurred during the update process. All changes must be
        /// saved back into the local memory, if its data hasn't changed yet.
        /// </summary>
        Error
    }


    /// <summary>
    /// Represents a change pair which is used to synchronize with a
    /// backend system.
    /// </summary>
    /// <typeparam name="TBucket">Specifies the type of the data bucket.</typeparam>
    public class CacheSavePair<TBucket> : ICacheSaveProgressFeedback
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Returns the synchronization result state for the current element.
        /// </summary>
        public CacheSavePairState State { get; set; }

        /// <summary>
        /// Gets the tracked bucket.
        /// </summary>
        public TBucket Bucket { get; private set; }

        /// <summary>
        /// Returns true if the current instance and its child elements
        /// are accepted on client (scrum table) side.
        /// </summary>
        public bool Accepted { get; set; }

        /// <summary>
        /// Gets a list of synchronization errors which caused the fail.
        /// </summary>
        public IList<CacheSaveError> Errors { get; private set; }

        /// <summary>
        /// Returns true if the current sync result contains error data.
        /// </summary>
        internal bool HasError { get { return (State == CacheSavePairState.Error); } }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new CacheSavePair instance.
        /// </summary>
        public CacheSavePair(TBucket bucket)
        {
            PreCondition.AssertNotNull(bucket, "bucket");

            Bucket = bucket;
            State = CacheSavePairState.Error;
            Errors = new List<CacheSaveError>();
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        #endregion
    }
}